package com.wzc.product.service.impl;

import com.wzc.basic.exception.BusinessException;
import com.wzc.basic.service.IBaiduAiAuditService;
import com.wzc.basic.service.impl.BaseServiceImpl;
import com.wzc.product.domain.Product;
import com.wzc.product.domain.ProductDetail;
import com.wzc.product.mapper.ProductDetailMapper;
import com.wzc.product.mapper.ProductMapper;
import com.wzc.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;



    @Override
    public void save(Product product) {
        // 审核文本
        Boolean textBoolean = baiduAiAuditService.textAudit(product.getName());
        // 审核图片
        Boolean imgBoolean = baiduAiAuditService.imageAudit(product.getResources());
        if(textBoolean && imgBoolean){
            productMapper.save(product);
            ProductDetail productDetail = product.getProductDetail();
            productDetail.setProduct_id(product.getId());
            productDetailMapper.save(productDetail);
        } else {
            throw new BusinessException("名字或图片包含敏感词汇！");
        }


    }

    @Override
    public void update(Product product) {
        // 审核文本
        Boolean textBoolean = baiduAiAuditService.textAudit(product.getName());
        // 审核图片
        Boolean imgBoolean = baiduAiAuditService.imageAudit(product.getResources());
        if(textBoolean && imgBoolean){
            productMapper.update(product);
            productDetailMapper.update(product.getProductDetail());
        } else {
            throw new BusinessException("名字或图片包含敏感词汇！");
        }


    }

    @Override
    public void onSale(List<Long> ids) {

        for(Long id : ids){
            Product product = productMapper.loadById(id);
            // 审核文本
            Boolean textBoolean = baiduAiAuditService.textAudit(product.getName());
            // 审核图片
            Boolean imgBoolean = baiduAiAuditService.imageAudit(product.getResources());

            if(textBoolean && imgBoolean){
                Date date = new Date();
                productMapper.offSale(ids,date);
            } else {
                throw new BusinessException("名字或图片包含敏感词汇！");
            }
        }
    }

    @Override
    public void offSale(List<Long> ids) {
        Date date = new Date();
        productMapper.onSale(ids,date);
    }
}
